package com.hfy.kandian.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hfy.kandian.dao.command.BookCommand;
import com.hfy.kandian.dao.representation.BookRepresentation;
import com.hfy.kandian.entity.Book;
import com.hfy.kandian.entity.Tag;
import com.hfy.kandian.entity.User;
import com.hfy.kandian.mapper.BookMapper;
import com.hfy.kandian.mapper.TagMapper;
import com.hfy.kandian.service.BookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author HAN XIAOYANG
 * @since 2020-12-30
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {
    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private TagMapper tagMapper;

    /**
     * 新建书籍
     */
    public int insertABook(BookCommand bookCommand) {
        Book book = bookCommand.convertToBook();
        return bookMapper.insert(book);
    }

    /**
     * 依据bookId查找书籍
     */

    public BookRepresentation getBookById(int bookId) {
        return bookMapper.selectById(bookId).convertToBookRepresentation();
    }

    @Override
    public List<BookRepresentation> getBookByKeyword(String keyword) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("book_name",keyword);
        List<Book> bookList = bookMapper.selectList(queryWrapper);
        List<BookRepresentation> bookRepresentationList = new ArrayList<>();
        for(Book thebook:bookList){
            bookRepresentationList.add(thebook.convertToBookRepresentation());
        }
        return bookRepresentationList;
    }

    @Override
    public int modifyBook(Book book) {
        return bookMapper.updateById(book);
    }

    @Override
    public List<String> showAllChapter(int bookId) {
        List<String> characterList = new ArrayList();
        Map ContentMap = bookMapper.selectById(bookId).getBookContentMap();
        ContentMap.forEach((k,v)->characterList.add(k.toString()));
        return characterList;

    }

    @Override
    public String showTheContent(int bookId,String chapterName) {
        Book theBook = bookMapper.selectById(bookId);
        return theBook.getBookContentMap().get(chapterName);
    }

    @Override
    public List<String> showAllTags(int bookId) {
        List<Integer> tagIdList = JSONArray.parseArray(bookMapper.selectById(bookId).getBookTag(),Integer.class);
        return tagMapper.selectBatchIds(tagIdList).stream().map(Tag::getTagName).collect(Collectors.toList());
    }

}